/**
 * jims
 */
package com.jims.master.modules.finance.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.*;
import com.jims.master.modules.doctor.service.*;
import com.jims.master.modules.finance.entity.RefundChargesApply;
import com.jims.master.modules.finance.service.OutpRcptMasterService;
import com.jims.master.modules.finance.service.RefundChargesApplyService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 退费申请记录Controller
 * @author 赵宁
 * @version 2017-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/refundChargesApply")
public class RefundChargesApplyController extends BaseController {

	@Autowired
	private RefundChargesApplyService refundChargesApplyService;
	@Autowired
	private ExamApplyService examApplyService;
	@Autowired
	private LabApplyService labApplyService;
	@Autowired
	private PrescriptionApplyService prescriptionApplyService;
	@Autowired
	private OutpTreatRecService outpTreatRecService;
	@Autowired
	private ClinicOrdersService clinicOrdersService;
	@Autowired
	private OutpRcptMasterService outpRcptMasterService;

	@ModelAttribute
	public RefundChargesApply get(@RequestParam(required=false) String id) {
		RefundChargesApply entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = refundChargesApplyService.get(id);
		}
		if (entity == null){
			entity = new RefundChargesApply();
		}
		return entity;
	}

	/**
	 * 保存退费申请记录
	 * @param refundChargesApply
	 * @param ordersId
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(RefundChargesApply refundChargesApply, String ordersId,String applyType,String confirmFlag) {
		StringData data=new StringData();
		refundChargesApply.setOrdersId(ordersId);
		ClinicOrders clinicOrders = clinicOrdersService.get(ordersId);
		String rcptNo = clinicOrders.getRcptNo();
        String chargeFlag="3";
        String type="1";
			if("0".equals(applyType)){ // 检查
				ExamApply examApply =new ExamApply();
				examApply.setOrdersId(ordersId);
				examApply.setChargeFlag(chargeFlag);
				examApplyService.refundIng(examApply);
			} else if("1".equals(applyType)){  // 检验
				LabApply labApply = new LabApply();
				labApply.setOrdersId(ordersId);
				labApply.setChargeFlag(chargeFlag);
				labApplyService.refundIng(labApply);
			} else if("3".equals(applyType)){  // 药品
				PrescriptionApply prescriptionApply = new PrescriptionApply();
				if(!"0".equals(confirmFlag)){ // 发药
                    type="2";
                    chargeFlag="5";
				}
                prescriptionApply.setOrdersId(ordersId);
                prescriptionApply.setChargeFlag(chargeFlag);
                prescriptionApplyService.refundIng(prescriptionApply);
			} else if("4".equals(applyType)){

			}else if("5".equals(applyType)){
                if("1".equals(confirmFlag)){
                    chargeFlag="5";
                    type="2";
                }
            }
        outpRcptMasterService.updateChargeFlag(rcptNo,chargeFlag, UserUtils.getOrgId());
        clinicOrdersService.refundIng(ordersId,type);
        refundChargesApply.setType(type);
		OutpTreatRec outpTreatRec = new OutpTreatRec();
		outpTreatRec.setOrdersId(ordersId);
		outpTreatRec.setChargeFlag(chargeFlag);
		outpTreatRecService.refundIng(outpTreatRec);
		int i=refundChargesApplyService.save(refundChargesApply);
		if(i>0){
			data.setCode("success");
			data.setData("保存退费申请记录成功");
		}else{
			data.setCode("error");
			data.setData("保存退费申请记录失败");
		}
		return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(RefundChargesApply refundChargesApply, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RefundChargesApply> page = refundChargesApplyService.findPage(new Page<RefundChargesApply>(request, response), refundChargesApply); 
		model.addAttribute("page", page);
		return "modules/finance/refundChargesApplyIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(RefundChargesApply refundChargesApply, Model model) {
		model.addAttribute("refundChargesApply", refundChargesApply);
		return "modules/finance/refundChargesApplyForm";
	}


	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(RefundChargesApply refundChargesApply) {
	    StringData data=new StringData();
		int i=refundChargesApplyService.delete(refundChargesApply);
        if(i>0){
            data.setCode("success");
            data.setData("删除退费申请记录成功");
        }else{
            data.setCode("error");
            data.setData("删除退费申请记录失败");
        }
		return data;
	}

}